using System;
using System.Collections.Generic;
using System.Linq;

namespace Quiki
{
    public class DocumentGraph
    {
        public IDictionary<WikiDocReference, WikiDocument> Documents
        {
            get { return documents; }
        }

        public void AddDocument(WikiDocument document)
        {
            documents.Add (document.Reference, document);
        }

        public IEnumerable<WikiDocument> FindAllDocuments (Func<WikiDocument, bool> predicate)
        {
            return documents.Values.AsQueryable().Where(predicate);
        }

        public WikiDocument FindDocument (WikiDocReference documentReference)
        {
            if (HasDocument(documentReference))
                return documents[documentReference];

            return null;
        }

        public bool HasDocument(WikiDocReference documentReference)
        {
            return documents.ContainsKey(documentReference);
        }

        public IEnumerable<WikiDocument> ListDocuments()
        {
            List<WikiDocument> sortedDocs = new List<WikiDocument>(documents.Values);
            sortedDocs.Sort (
                (a, b) => string.Compare(a.Title, b.Title, StringComparison.CurrentCultureIgnoreCase));
            return sortedDocs;
        }

        private readonly Dictionary<WikiDocReference, WikiDocument> documents = new Dictionary<WikiDocReference, WikiDocument> ();
    }
}